"use strict";

/**
 * Created by xuchao on 15/10/16.
 */
angular.module('app.controller')
  .controller('DetailCtrl', function ($scope, detail, RestService, Toast, $state, $stateParams) {
    $scope.detail = detail;
    $scope.imgList = angular.fromJson(detail.material);

    $scope.desc = angular.fromJson(detail.item.replace(/\n/g, '')).desc;

    $scope.back = function () {
      $state.go('home', {openid: $stateParams.openid, activityId:$stateParams.activityId});
    };

    $scope.vote = function () {
      if($stateParams.openid){
        $scope.loading = true;
        RestService.vote($stateParams.activityId, $stateParams.openid, $scope.detail.id).$promise.then(function (data) {
          if (data.errCode == 1) {
            Toast.success('投票成功');
            $state.go('home', {openid: $stateParams.openid, activityId: $stateParams.activityId});
          } else {
            Toast.fail(data.errMsg);
          }
        }).finally(function () {
          $scope.loading = false;
        });
      }else{
        $scope.noId = true;
      }
    };

    (function () {
      var timeline = {
        title: '德州移动精英店长评选活动',
        link: 'http://activity.weixin.cloudatum.com/activity/dzmanager/index.html#/?activityId=' + $stateParams.activityId + '&itemId=' + $scope.detail.id,
        imgUrl: $scope.imgList[0].imageUrl
      };
      var message = {
        title: '德州移动精英店长评选活动',
        desc: '德州移动精英店长火爆评选中,内藏帅哥美女快来瞅瞅你认识几个',
        link: 'http://activity.weixin.cloudatum.com/activity/dzmanager/index.html#/?activityId=' + $stateParams.activityId + '&itemId=' + $scope.detail.id,
        imgUrl: $scope.imgList[0].imageUrl
      };
      wx.onMenuShareTimeline(timeline);
      wx.onMenuShareAppMessage(message);
    })();
  });
